package com.hsmw.api.service;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.mapper.HsmwAuditMapper;
import com.hsmw.api.mapper.HsmwBanDriverMapper;
import com.hsmw.api.mapper.HsmwScoringRecordMapper;
import com.hsmw.api.vo.DriverInfoDTO;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwBanDriverService extends BaseService<HsmwBanDriverMapper, HsmwBanDriver> {

    @Autowired
    private HsmwBanDriverMapper hsmwBanDriverMapper;

    @Autowired
    private HsmwUserDriverService hsmwUserDriverService;



    @Autowired
    private HsmwReportRecordService hsmwReportRecordService;


    @Autowired
    private HsmwDriverUserService hsmwDriverUserService;

    @Autowired
    private HsmwScoringRecordMapper hsmwScoringRecordMapper;


    /**
     * 新增修改信息
     * @param hsmwBanDrivers
     * @return
     */
    public DataResponse saveOrUpdateBanDriver(List<HsmwBanDriver> hsmwBanDrivers) {

        if(CollectionUtil.isEmpty(hsmwBanDrivers)){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"传递参数不能为空");
        }

        for (HsmwBanDriver hsmwBanDriver : hsmwBanDrivers) {
            //修改
            if(StringUtils.isNotBlank(hsmwBanDriver.getId())){
                hsmwBanDriverMapper.updateById(hsmwBanDriver);
            }else {
                //新增
                if(StringUtils.isNotBlank(hsmwBanDriver.getDuration())){
                    LocalDateTime now = LocalDateTime.now();
                    // 封禁时长为 1天
                    if(StringUtils.equals("1",hsmwBanDriver.getDuration())){
                        hsmwBanDriver.setDurationEndTime(now.plusDays(Integer.parseInt(hsmwBanDriver.getDuration())));
                    }
                    if(StringUtils.equals("3",hsmwBanDriver.getDuration())){
                        hsmwBanDriver.setDurationEndTime(now.plusDays(Integer.parseInt(hsmwBanDriver.getDuration())));
                    }
                    if(StringUtils.equals("5",hsmwBanDriver.getDuration())){
                        hsmwBanDriver.setDurationEndTime(now.plusDays(Integer.parseInt(hsmwBanDriver.getDuration())));
                    }
                    if(StringUtils.equals("7",hsmwBanDriver.getDuration())){
                        hsmwBanDriver.setDurationEndTime(now.plusDays(Integer.parseInt(hsmwBanDriver.getDuration())));
                    }
                    if(StringUtils.equals("10",hsmwBanDriver.getDuration())){
                        hsmwBanDriver.setDurationEndTime(now.plusDays(Integer.parseInt(hsmwBanDriver.getDuration())));
                    }
                    if(StringUtils.isBlank(hsmwBanDriver.getDeductPoints())){
                        return new DataResponse("积分传递错误");
                    }
                    HsmwUserDriver userDriver = hsmwUserDriverService.getById(hsmwBanDriver.getDriverId());
                    if(userDriver == null){
                        return new DataResponse("没有该司机信息");
                    }
                    if(!StringUtils.equals(userDriver.getWorkStatus(),CommonConstants.DRIVER_WORK)){

                        String driverId = hsmwBanDriver.getDriverId();

                        List<HsmwBanDriver> drivers = hsmwBanDriverMapper.selectList(new LambdaQueryWrapper<HsmwBanDriver>().eq(HsmwBanDriver::getDriverId, driverId).eq(HsmwBanDriver::getStatus,"0"));
                        if(CollectionUtil.isNotEmpty(drivers)){
                            return new DataResponse("司机已有封禁信息");
                        }
                        HsmwScoringRecord scoringRecord = new HsmwScoringRecord();
                        scoringRecord.setDriverId(userDriver.getId());
                        scoringRecord.setInitialScore(userDriver.getScore());
                        scoringRecord.setEndScore(String.valueOf(Long.parseLong(userDriver.getScore()) - Integer.parseInt(hsmwBanDriver.getDeductPoints())));
                        scoringRecord.setFluctuateScore("-"+hsmwBanDriver.getDeductPoints());
                        scoringRecord.setRemark("封禁扣除评分");
                        hsmwScoringRecordMapper.insert(scoringRecord);

                        userDriver.setScore(String.valueOf(Integer.parseInt(userDriver.getScore()) - Integer.parseInt(hsmwBanDriver.getDeductPoints())));
                        userDriver.setWorkStatus(CommonConstants.BAN_WORK);
                        hsmwBanDriver.setStatus("1");
                        hsmwUserDriverService.updateById(userDriver);
                        hsmwBanDriverMapper.insert(hsmwBanDriver);
                        HsmwReportRecord hsmwReportRecord = new HsmwReportRecord();
                        if(StringUtils.isNotBlank(userDriver.getUserId())){
                            HsmwDriverUser user = hsmwDriverUserService.getById(userDriver.getUserId());
                            hsmwReportRecord.setDriverName(user.getName());
                        }
                        hsmwReportRecord.setDuration(hsmwBanDriver.getDuration());
                        hsmwReportRecord.setDeductPoints(hsmwBanDriver.getDeductPoints());
                        hsmwReportRecord.setReportId(hsmwBanDriver.getComplainId());
                        hsmwReportRecordService.save(hsmwReportRecord);
                        return new DataResponse(CommonConstants.ResponseStatus.SUCCESS,"封禁司机成功");
                    }else {
                        String driverId = hsmwBanDriver.getDriverId();
                        List<HsmwBanDriver> drivers = hsmwBanDriverMapper.selectList(new LambdaQueryWrapper<HsmwBanDriver>().eq(HsmwBanDriver::getDriverId, driverId).eq(HsmwBanDriver::getStatus,"0"));
                        if(CollectionUtil.isNotEmpty(drivers)){
                            return new DataResponse("司机已有封禁信息");
                        }
                        hsmwBanDriverMapper.insert(hsmwBanDriver);
                        return new DataResponse("封禁司机失败，该司机正在工作中,订单结束后司机会自动封禁！");
                    }
                }
            }

        }

        return new DataResponse();
    }

    public List<DriverInfoDTO> findDriverListByOrderId(String id) {


        return hsmwBanDriverMapper.findDriverListByOrderId(id);
    }
}
